/* 
 * Amnesia is Copyright (c) 2010 Mark Russell
 * 
 * Contact: info@amnesia-app.com	
 * 
 * This file is part of Amnesia.
 * 
 * Amnesia is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Amnesia is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Amnesia. If not, see <http://www.gnu.org/licenses/>.
 *
 */

// -------------------------------------------------------------
// Amnesia Application Code
// -------------------------------------------------------------

// Globals

var myKey = "";
var cat_name = "";
var item_no = "0";
var field_count = "";
var viewport_height;
var stat_msg = "";

// -------------------------------------------------------------
// Miscellaneous functions
// -------------------------------------------------------------

// Javascript nl2br

function nl2br (str,state) {
	if (state){
		// convert line endings to tags
		var breakTag = '<br/>';
    	return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
	} else {
		// convert tags to line endings
		var breakTag = '\r';
		return (str + '').replace(/<br\/>/g, '' + breakTag);
	}
}


// Clear all status messages

function clear_status(){
	$(".error").hide();
	$(".col4_row_edit_error").hide();
	$("#amnesia_locked").hide();
	$("#write_success").hide();
	$("#new_success").hide();
	$("#delete_success").hide();
	$("#item_blank").hide();
	$("#username_copied").hide();
	$("#password_copied").hide();
	$("#serial_copied").hide();
	$("#cardno_copied").hide();
}

// Make all usable vars "" vars on setup and lock
function reset_vars(){
	
	var cat_array = new Array("web", "hosted", "phone", "license", "cards", "banking", "email", "computer_user", "hardware", "certificates", "notes");
	var state_array = new Array("_r", "_e", "_a", "_un", "_aun");
	var current_cat;
	var current_state;
	var full_cat;

	for (var i = 0; i < 5; i++){
		current_state = state_array[i];

		for (var j = 0; j < 11; j++){
			current_cat = cat_array[j] + current_state;
			
			for (var k = 0; k < 9; k++){
				full_cat = current_cat + k;
				eval(full_cat+"=''");
			}
		}
	}
}

// Increment/decrement user's count

function update_count(added){

	if(added){
		user_count +=1;
	} else {
		user_count -=1;
	}
	
	var count_query = "user=" + user + "&count_value=" + user_count;
	
	$.ajax({
			type: "POST",
			url: "app/count.php",
			data: count_query,
			success: function() {
				if (user_count ==0) {
					$("#user_count").replaceWith("<span id=\"user_count\">Amnesia remembers things so you can forget them. Start by adding some items.</span>");
				} else if (user_count == 1) {
					$("#user_count").replaceWith("<span id=\"user_count\">Amnesia has remembered " + user_count + " things so you don't have to.</span>");
				} else {
					$("#user_count").replaceWith("<span id=\"user_count\">Amnesia has remembered " + user_count + " things so you don't have to.</span>");
				}
			}
		});
}

// -------------------------------------------------------------
// Provide the correct table and div names for each category
// -------------------------------------------------------------

// Set default category
var category = "1";
var cat_name = "web";
var field_count = 5;

// Sets up basic category-specific details
function update_category(cat){
	if (cat == "1"){
		category = "1";
		cat_name = "web";
		field_count = 5;
		item_no = "0";
	} else if (cat == "2"){
		category = "2";
		cat_name = "hosted";
		field_count = 9;
		item_no = "0";
	} else if (cat == "3"){
		category = "3";
		cat_name = "phone";
		field_count = 4;
		item_no = "0";
	} else if (cat == "4"){
		category = "4";
		cat_name = "license";
		field_count = 4;
		item_no = "0";
	} else if (cat == "5"){
		category = "5";
		cat_name = "cards";
		field_count = 9;
		item_no = "0";
	} else if (cat == "6"){
		category = "6";
		cat_name = "banking";
		field_count = 4;
		item_no = "0";
	} else if (cat == "7"){
		category = "7";
		cat_name = "email";
		field_count = 6;
		item_no = "";
	} else if (cat == "8"){
		category = "8";
		cat_name = "computer_user";
		field_count = 5;
		item_no = "0";
	} else if (cat == "9"){
		category = "9";
		cat_name = "hardware";
		field_count = 7;
		item_no = "0";
	} else if (cat == "10"){
		category = "10";
		cat_name = "certificates";
		field_count = 6;
		item_no = "0";
	} else if (cat == "11"){
		category = "11";
		cat_name = "notes";
		field_count = 4;
		item_no = "0";
	}
}

// Reset if main list used
function reset_right(){
	clear_status();
	$("#read_" + cat_name).hide();
	$("#edit_" + cat_name).hide();
	$("#add_" + cat_name).hide();
	$("#detail_empty").show();
}

$(document).ready(function() {
	// -------------------------------------------------------------
	// Setup page
	// -------------------------------------------------------------
	
	function setup_page(){
		$("#master_key").fadeIn("fast");
		$("#key_message").fadeIn("fast");
		$("input#master_key_input").val("");
		$("#master_key_input").focus();
		track_viewport_height();
		resize_scrollers()
		reset_vars();
	}
	
	setup_page();
	
	// -------------------------------------------------------------
	// Extract and build the category list
	// -------------------------------------------------------------
	
	function get_categories(user,category){
		
		clear_status();
		$(".loading").show();
	
		var cat_list = "";
		var category_id = "";
	
		// Ajax call
		$.post("app/category_list.php", { user_id: user }, function(catXML){
			// Loop XML into variable 'data' for each attribute
			$('category',catXML).each(function(i){					
				// Extract each XML variable 
				category_id = $(this).find("category_id").text();
				var category_name = $(this).find("name").text();
				var category_table = $(this).find("item_table").text();
				var category_icon = $(this).find("icon_path").text();
				
				// Work the list into useful HTML
				cat_list += "<li><a class=\"cat_list_link\" id=\"cat_" + category_id + "\" href=\"javascript:void(0)\"><img class=\"icon\" src=\"../img/icons/" + category_icon + "\" alt=\"" + category_name + "\" width=\"16\" height=\"16\"/>" + category_name + "</a></li>";
			});
			
			$("ul#cat_list").replaceWith("<ul id=\"cat_list\">" + cat_list + "</ul>");
			get_list("",category);
			
			// Highlight default category in list on load
			$("#cat_" + category).css("background-color","#a1a7ae");
			$("#cat_" + category).css("color","#fff");
			
			// Category click events
			$("#cat_1").click(function() {
				reset_right();
				category = "1";
				update_category("1");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_2").click(function() {
				reset_right();
				category = "2";
				update_category("2");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_3").click(function() {
				reset_right();
				category = "3";
				update_category("3");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_4").click(function() {
				reset_right();
				category = "4";
				update_category("4");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_5").click(function() {
				reset_right();
				category = "5";
				update_category("5");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_6").click(function() {
				reset_right();
				category = "6";
				update_category("6");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_7").click(function() {
				reset_right();
				category = "7";
				update_category("7");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_8").click(function() {
				reset_right();
				category = "8";
				update_category("8");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_9").click(function() {
				reset_right();
				category = "9";
				update_category("9");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_10").click(function() {
				reset_right();
				category = "10";
				update_category("10");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			$("#cat_11").click(function() {
				reset_right();
				category = "11";
				update_category("11");
				$(".cat_list_link").css("background-color","");
				$(".cat_list_link").css("color","#31373f");
				$("#col4_title_read").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_empty").show();
				$(this).css("background-color","#a1a7ae");
				$(this).css("color","#fff");
				get_list(0,category);
				time_out();
			});
			
		});
		
		$(".loading").hide();
	}
	
	// -------------------------------------------------------------
	// Extract and decode the main item list
	// -------------------------------------------------------------
	
	
	// Add highlight styles to active item	
	function highlight_active_item(item_no){
		$(".item").css("background-color","");
		$(".item").css("color","#31373f");
		$(".item").css("border-left","1px solid #fff");
		$("#" + item_no).css("background-color","#a1a7ae");
		$("#" + item_no).css("color","#fff");
		$("#" + item_no).css("border-left","1px solid #babec3");
	}
	
	// Construct the list
	function get_list(populate,category,stat_msg){
	
		clear_status();
		$(".loading").show();

		var list = "";
		var list_item = "";
		var max_string = 25;
		
		$(".item_list").replaceWith("<div class=\"item_list\"></div>");
		
		$.post("app/list.php", { user_id: user, cat_name: cat_name}, function(listXML){
			// Loop XML into variable 'data' for each item
			$('item',listXML).each(function(i){					
				// Extract each XML variable
				var item = $(this).find("id").text();
				var encoded_title = $(this).find("title").text();
								
				// Decode item name using myKey
				list_item = Passpack.decode("AES",encoded_title,myKey);
				
				// Work the list into useful HTML
				if (list_item.length <= max_string){
					list += "<li><a id=\"" + item + "\" class=\"item\" href=\"javascript:void(0)\">" + list_item + "</a></li>";
				} else {
					list += "<li><a id=\"" + item + "\" class=\"item\" href=\"javascript:void(0)\" title=\"" + list_item + "\">" + list_item + "</a></li>";
				}
				
			});
			// Construct the output info (for both read and write) then sort it alphabetically.
			$("#item_list").replaceWith("<ul id=\"item_list\">" + list + "</ul>");
			
			// Truncate if over 25 chars
			$(".item").truncate(max_string);
			
			// Alphabetically sort the list
			$("#item_list>li").tsort();
			
			// Item click event - Call encoded values from db, decode and display in read mode
			$(".item").click(function() {
				item_no = $(this).attr('id');
				read_db(category,item_no);
				$("#write_success").hide();
				$("#delete_success").hide();
				$("#new_success").hide();
				$("#detail_empty").hide();
				$("#col4_title_empty").hide();
				$("#col4_title_edit").hide();
				$("#col4_title_add").hide();
				$("#col4_title_read").show();
				$("#edit_" + cat_name).hide();
				$("#add_" + cat_name).hide();
				$("#read_" + cat_name).show();
				highlight_active_item(item_no);
			});
			if (item_no){
				highlight_active_item(item_no);
			}
		});
		
		$(".loading").hide();
		
		// Reinstate relevant status message if applicable
		if (stat_msg == "delete") {
			$("#delete_success").show();
		}
	}
	
	// -------------------------------------------------------------
	// Read Mode - Decrypt and show data into read-only HTML
	// -------------------------------------------------------------
	
	

	// The main item db read
	function read_db(category,item_no,stat_msg){
		
		// Show the loading icon
		clear_status();
		$(".loading").show();
		
		// Copy to clipboard notification
		var clip1 = new ZeroClipboard.Client();
		var clip2 = new ZeroClipboard.Client();
		var clip3 = new ZeroClipboard.Client();
		var clip4 = new ZeroClipboard.Client();
		var clip5 = new ZeroClipboard.Client();
		var clip6 = new ZeroClipboard.Client();
		var clip7 = new ZeroClipboard.Client();
		var clip8 = new ZeroClipboard.Client();
		var clip9 = new ZeroClipboard.Client();
		var clip10 = new ZeroClipboard.Client();
		var clip11 = new ZeroClipboard.Client();
		var clip12 = new ZeroClipboard.Client();
		var clip13 = new ZeroClipboard.Client();
		var clip14 = new ZeroClipboard.Client();
		
		clip1.addEventListener( 'onComplete', clip_username_complete );
		clip2.addEventListener( 'onComplete', clip_password_complete );
		clip3.addEventListener( 'onComplete', clip_username_complete );
		clip4.addEventListener( 'onComplete', clip_password_complete );
		clip5.addEventListener( 'onComplete', clip_username_complete );
		clip6.addEventListener( 'onComplete', clip_password_complete );
		clip7.addEventListener( 'onComplete', clip_username_complete );
		clip8.addEventListener( 'onComplete', clip_password_complete );
		clip9.addEventListener( 'onComplete', clip_serial_complete );
		clip10.addEventListener( 'onComplete', clip_cardno_complete );
		clip11.addEventListener( 'onComplete', clip_username_complete );
		clip12.addEventListener( 'onComplete', clip_password_complete );
		clip13.addEventListener( 'onComplete', clip_username_complete );
		clip14.addEventListener( 'onComplete', clip_password_complete );
	        
	    function clip_username_complete( client, text ) {
	    	clear_status();
	        $("#username_copied").show();
	    }
	    
	    function clip_password_complete( client, text ) {
	    	clear_status();
	        $("#password_copied").show();
	    }
	    
	    function clip_serial_complete( client, text ) {
	    	clear_status();
	        $("#serial_copied").show();
	    }
	    
	    function clip_cardno_complete( client, text ) {
	    	clear_status();
	        $("#cardno_copied").show();
	    }
		
		var data = "";
		
		// Ajax call
		$.post("app/read.php", { user_id: user, cat_id: category, item_id: item_no }, function(theXML){
			// Loop XML into variable 'data' for each attribute
			$('item',theXML).each(function(i){
			
				// Extract each XML variable
				
				if (category=="1"){
					// web
					var encoded_title = $(this).find("title").text();
					var encoded_username = $(this).find("username").text();
					var encoded_password = $(this).find("password").text();
					var encoded_url = $(this).find("url").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					web_r0 = Passpack.decode("AES",encoded_title,myKey);
					web_r1 = Passpack.decode("AES",encoded_username,myKey);
					web_r2 = Passpack.decode("AES",encoded_password,myKey);
					web_r3 = Passpack.decode("AES",encoded_url,myKey);
					web_r4 = Passpack.decode("AES",encoded_notes,myKey);
					
					// Add copy to clipboard functionality
					clip1.destroy();
					clip2.destroy();
					clip1.setText(web_r1);
					clip1.glue('copy_username_image_web','copy_username_holder_web');
					clip2.setText(web_r2);
					clip2.glue('copy_password_image_web','copy_password_holder_web');
					
				} else if (category=="2"){
					// hosted
					var encoded_title = $(this).find("title").text();
					var encoded_username = $(this).find("username").text();
					var encoded_password = $(this).find("password").text();
					var encoded_dns1 = $(this).find("dns1").text();
					var encoded_dns2 = $(this).find("dns2").text();
					var encoded_dns3 = $(this).find("dns3").text();
					var encoded_dns4 = $(this).find("dns4").text();
					var encoded_ip = $(this).find("ip").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					hosted_r0 = Passpack.decode("AES",encoded_title,myKey);
					hosted_r1 = Passpack.decode("AES",encoded_username,myKey);
					hosted_r2 = Passpack.decode("AES",encoded_password,myKey);
					hosted_r3 = Passpack.decode("AES",encoded_dns1,myKey);
					hosted_r4 = Passpack.decode("AES",encoded_dns2,myKey);
					hosted_r5 = Passpack.decode("AES",encoded_dns3,myKey);
					hosted_r6 = Passpack.decode("AES",encoded_dns4,myKey);
					hosted_r7 = Passpack.decode("AES",encoded_ip,myKey);
					hosted_r8 = Passpack.decode("AES",encoded_notes,myKey);
					
					// Add copy to clipboard functionality
					clip5.destroy();
					clip6.destroy();
					clip5.setText(hosted_r1);
					clip5.glue('copy_username_image_hosted','copy_username_holder_hosted');
					clip6.setText(hosted_r2);
					clip6.glue('copy_password_image_hosted','copy_password_holder_hosted');
					
				} else if (category=="3"){
					// phone
					var encoded_title = $(this).find("title").text();
					var encoded_username = $(this).find("username").text();
					var encoded_password = $(this).find("password").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					phone_r0 = Passpack.decode("AES",encoded_title,myKey);
					phone_r1 = Passpack.decode("AES",encoded_username,myKey);
					phone_r2 = Passpack.decode("AES",encoded_password,myKey);
					phone_r3 = Passpack.decode("AES",encoded_notes,myKey);
					
					// Add copy to clipboard functionality
					clip7.destroy();
					clip8.destroy();
					clip7.setText(phone_r1);
					clip7.glue('copy_username_image_phone','copy_username_holder_phone');
					clip8.setText(phone_r2);
					clip8.glue('copy_password_image_phone','copy_password_holder_phone');
					
				} else if (category=="4"){
					// license
					var encoded_title = $(this).find("title").text();
					var encoded_serial = $(this).find("serial").text();
					var encoded_url = $(this).find("url").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					license_r0 = Passpack.decode("AES",encoded_title,myKey);
					license_r1 = Passpack.decode("AES",encoded_serial,myKey);
					license_r2 = Passpack.decode("AES",encoded_url,myKey);
					license_r3 = Passpack.decode("AES",encoded_notes,myKey);
					
					// Add copy to clipboard functionality
					clip9.destroy();
					clip9.setText(license_r1);
					clip9.glue('copy_serial_image_license','copy_serial_holder_license');
					
				} else if (category=="5"){
					// cards
					var encoded_title = $(this).find("title").text();
					var encoded_service = $(this).find("service").text();
					var encoded_card_no = $(this).find("card_no").text();
					var encoded_start = $(this).find("start").text();
					var encoded_end = $(this).find("end").text();
					var encoded_cvn = $(this).find("cvn").text();
					var encoded_name = $(this).find("name").text();
					var encoded_pin = $(this).find("pin").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					cards_r0 = Passpack.decode("AES",encoded_title,myKey);
					cards_r1 = Passpack.decode("AES",encoded_service,myKey);
					cards_r2 = Passpack.decode("AES",encoded_card_no,myKey);
					cards_r3 = Passpack.decode("AES",encoded_start,myKey);
					cards_r4 = Passpack.decode("AES",encoded_end,myKey);
					cards_r5 = Passpack.decode("AES",encoded_cvn,myKey);
					cards_r6 = Passpack.decode("AES",encoded_name,myKey);
					cards_r7 = Passpack.decode("AES",encoded_pin,myKey);
					cards_r8 = Passpack.decode("AES",encoded_notes,myKey);
					
					// Add copy to clipboard functionality
					clip10.destroy();
					clip10.setText(cards_r2);
					clip10.glue('copy_cardno_image_cards','copy_cardno_holder_cards');
					
				} else if (category=="6"){
					// banking
					var encoded_title = $(this).find("title").text();
					var encoded_username = $(this).find("username").text();
					var encoded_password = $(this).find("password").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					banking_r0 = Passpack.decode("AES",encoded_title,myKey);
					banking_r1 = Passpack.decode("AES",encoded_username,myKey);
					banking_r2 = Passpack.decode("AES",encoded_password,myKey);
					banking_r3 = Passpack.decode("AES",encoded_notes,myKey);
					
					// Add copy to clipboard functionality
					clip3.destroy();
					clip4.destroy();
					clip3.setText(banking_r1);
					clip3.glue('copy_username_image_banking','copy_username_holder_banking');
					clip4.setText(banking_r2);
					clip4.glue('copy_password_image_banking','copy_password_holder_banking');
					
				} else if (category=="7"){
					// email
					var encoded_title = $(this).find("title").text();
					var encoded_username = $(this).find("username").text();
					var encoded_password = $(this).find("password").text();
					var encoded_server = $(this).find("server").text();
					var encoded_smtp = $(this).find("smtp").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					email_r0 = Passpack.decode("AES",encoded_title,myKey);
					email_r1 = Passpack.decode("AES",encoded_username,myKey);
					email_r2 = Passpack.decode("AES",encoded_password,myKey);
					email_r3 = Passpack.decode("AES",encoded_server,myKey);
					email_r4 = Passpack.decode("AES",encoded_smtp,myKey);
					email_r5 = Passpack.decode("AES",encoded_notes,myKey);
					
					// Add copy to clipboard functionality
					clip11.destroy();
					clip12.destroy();
					clip11.setText(email_r1);
					clip11.glue('copy_username_image_email','copy_username_holder_email');
					clip12.setText(email_r2);
					clip12.glue('copy_password_image_email','copy_password_holder_email');
					
				} else if (category=="8"){
					// computer_user
					var encoded_title = $(this).find("title").text();
					var encoded_username = $(this).find("username").text();
					var encoded_password = $(this).find("password").text();
					var encoded_platform = $(this).find("platform").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					computer_user_r0 = Passpack.decode("AES",encoded_title,myKey);
					computer_user_r1 = Passpack.decode("AES",encoded_username,myKey);
					computer_user_r2 = Passpack.decode("AES",encoded_password,myKey);
					computer_user_r3 = Passpack.decode("AES",encoded_platform,myKey);
					computer_user_r4 = Passpack.decode("AES",encoded_notes,myKey);
					
					// Add copy to clipboard functionality
					clip13.destroy();
					clip14.destroy();
					clip13.setText(computer_user_r1);
					clip13.glue('copy_username_image_computer_user','copy_username_holder_computer_user');
					clip14.setText(computer_user_r2);
					clip14.glue('copy_password_image_computer_user','copy_password_holder_computer_user');
					
				} else if (category=="9"){
					// hardware
					var encoded_title = $(this).find("title").text();
					var encoded_model = $(this).find("model").text();
					var encoded_serial = $(this).find("serial").text();
					var encoded_location = $(this).find("location").text();
					var encoded_name = $(this).find("name").text();
					var encoded_tag = $(this).find("tag").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					hardware_r0 = Passpack.decode("AES",encoded_title,myKey);
					hardware_r1 = Passpack.decode("AES",encoded_model,myKey);
					hardware_r2 = Passpack.decode("AES",encoded_serial,myKey);
					hardware_r3 = Passpack.decode("AES",encoded_location,myKey);
					hardware_r4 = Passpack.decode("AES",encoded_name,myKey);
					hardware_r5 = Passpack.decode("AES",encoded_tag,myKey);
					hardware_r6 = Passpack.decode("AES",encoded_notes,myKey);
					
				} else if (category=="10"){
					// certificates
					var encoded_title = $(this).find("title").text();
					var encoded_company = $(this).find("company").text();
					var encoded_url = $(this).find("url").text();
					var encoded_email = $(this).find("email").text();
					var encoded_certificate = $(this).find("certificate").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					certificates_r0 = Passpack.decode("AES",encoded_title,myKey);
					certificates_r1 = Passpack.decode("AES",encoded_company,myKey);
					certificates_r2 = Passpack.decode("AES",encoded_url,myKey);
					certificates_r3 = Passpack.decode("AES",encoded_email,myKey);
					certificates_r4 = Passpack.decode("AES",encoded_certificate,myKey);
					certificates_r5 = Passpack.decode("AES",encoded_notes,myKey);
					
				} else if (category=="11"){
					// notes
					var encoded_title = $(this).find("title").text();
					var encoded_date = $(this).find("date").text();
					var encoded_updated = $(this).find("updated").text();
					var encoded_notes = $(this).find("notes").text();
					
					// Decode each using myKey
					notes_r0 = Passpack.decode("AES",encoded_title,myKey);
					notes_r1 = encoded_date;
					notes_r2 = encoded_updated;
					notes_r3 = Passpack.decode("AES",encoded_notes,myKey);
				}
				
			});
			// Construct the output info (for both read and write)		
			
			for (var i = 0; i < field_count; i++){
			
			
			
				data = eval(cat_name + "_r" + i);
				if (cat_name == "web" | cat_name == "computer_user" | cat_name == "email" | cat_name == "hosted" | cat_name == "banking" | cat_name == "phone" && i == 2){
					$("." + cat_name + "_r" + i).replaceWith("<span class=\"" + cat_name + "_r" + i + "\">&bull; &bull; &bull; &bull; &bull; &bull;</span>");
				} else if (cat_name == "web" && i == 3){
					
					// Pattern-match string and add 'http://' if necessary.
					$("." + cat_name + "_r" + i).replaceWith("<span class=\"" + cat_name + "_r" + i + "\"><a class=\"text_link\" href=\"" + data + "\" target=\"_blank\" title=\"Visit site\">" + data + "</a></span>");
					
					// And now add the visit button only if there's a URL associated with the item
					if (data){
					
						// Add a favicon before the title if there's a URL
						$('.text_link').filter(function(){
							return this.hostname && this.hostname !== location.hostname;
						}).each(function() {
							var link = jQuery(this);
							var title = $("." + cat_name + "_r0");
							var faviconURL =
								link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1')+'/favicon.ico';
							var faviconIMG = $('<img class="added_favicon" width="16" height="16" src="../img/icons/web.png" alt="" />')['prependTo'](title);
							var extImg = new Image();
							extImg.src = faviconURL;
							if (extImg.complete)
								faviconIMG.attr('src', faviconURL);
							else
								extImg.onload = function() { faviconIMG.attr('src', faviconURL); };
						});
						
						// Also add the link to the visit button					
						$("." + cat_name + "_visit_button").replaceWith("<a class=\"" + cat_name + "_visit_button\" href=\"" + data + "\" title=\"Visit Site\"target=\"_blank\"><img src=\"../img/buttons/visit_icn.png\" alt=\"Visit site\" width=\"22\" height=\"22\"/></a>");
						
					} else {
						// Don't show the visit button at all
						$("." + cat_name + "_visit_button").replaceWith("<a class=\"" + cat_name + "_visit_button\"></a>");
					}
					
				} else {
					$("." + cat_name + "_r" + i).replaceWith("<span class=\"" + cat_name + "_r" + i + "\">" + data + "</span>");
				}
								
				// Populate the edit fields
				$("." + cat_name + "_e" + i).attr("value", nl2br(data,0));
			}
			
		});
		
		// Reset the show/hide password toggle
		clear_status();
		$(".pwd_hide").hide();
		$(".pwd_show").show();
		
		// Reinstate relevant status message if applicable
		if (stat_msg == "edit") {
			$("#write_success").show();
		} else if (stat_msg == "add") {
			$("#new_success").show();
		}
		
		// Hide the loading icon
		$(".loading").hide();
		
		time_out();
	}
	
	// -------------------------------------------------------------
	// Edit Mode - Show form and update db in editable HTML form
	// -------------------------------------------------------------
	
	
	$(".overwrite").click(function() {
	
		// Show the loading icon
		clear_status();
		$(".loading").show();

		var queryString = "";
		
		// validate and process form here
		
		if (category=="1"){
			// web
			web_un0 = $("input.web_e0").attr('value');
			web_un1 = $("input.web_e1").attr('value');
			web_un2 = $("input.web_e2").attr('value');
			web_un3 = $("input.web_e3").attr('value');
			
			// add http://, if missing, to make a valid URL
			var http_regex = /^http[s]?:\/\//;
			
			if (web_un3 == ""){
				web_un3 = web_un3;
			} else if (!web_un3.match(http_regex)){
				web_un3 = "http://" + web_un3;
			}
			
			web_un4 = nl2br($("textarea.web_e4").val(),1);
			
			// Encode each using myKey
      		web_e0 = Passpack.encode("AES",web_un0,myKey);
	      	web_e1 = Passpack.encode("AES",web_un1,myKey);
	      	web_e2 = Passpack.encode("AES",web_un2,myKey);
	      	web_e3 = Passpack.encode("AES",web_un3,myKey);
			web_e4 = Passpack.encode("AES",web_un4,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ web_e0 + '&v2=' + web_e1 + '&v3=' + web_e2 + '&v4=' + web_e3 + '&v5=' + web_e4;
	      	
	      	//echo(id);
		
		} else if (category=="2"){
			// hosted
			hosted_un0 = $("input.hosted_e0").attr('value');
			hosted_un1 = $("input.hosted_e1").attr('value');
			hosted_un2 = $("input.hosted_e2").attr('value');
			hosted_un3 = $("input.hosted_e3").attr('value');
			hosted_un4 = $("input.hosted_e4").attr('value');
			hosted_un5 = $("input.hosted_e5").attr('value');
			hosted_un6 = $("input.hosted_e6").attr('value');
			hosted_un7 = $("input.hosted_e7").attr('value');
			hosted_un8 = nl2br($("textarea.hosted_e8").val(),1);
			
			// Encode each using myKey
      		hosted_e0 = Passpack.encode("AES",hosted_un0,myKey);
	      	hosted_e1 = Passpack.encode("AES",hosted_un1,myKey);
	      	hosted_e2 = Passpack.encode("AES",hosted_un2,myKey);
	      	hosted_e3 = Passpack.encode("AES",hosted_un3,myKey);
	      	hosted_e4 = Passpack.encode("AES",hosted_un4,myKey);
	      	hosted_e5 = Passpack.encode("AES",hosted_un5,myKey);
	      	hosted_e6 = Passpack.encode("AES",hosted_un6,myKey);
	      	hosted_e7 = Passpack.encode("AES",hosted_un7,myKey);
	      	hosted_e8 = Passpack.encode("AES",hosted_un8,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ hosted_e0 + '&v2=' + hosted_e1 + '&v3=' + hosted_e2 + '&v4=' + hosted_e3 + '&v5=' + hosted_e4 + '&v6=' + hosted_e5 + '&v7=' + hosted_e6 + '&v8=' + hosted_e7 + '&v9=' + hosted_e8;
		
		} else if (category=="3"){
			// phone

			phone_un0 = $("input.phone_e0").attr('value');
			phone_un1 = $("input.phone_e1").attr('value');
			phone_un2 = $("input.phone_e2").attr('value');
			phone_un3 = nl2br($("textarea.phone_e3").val(),1);
			
			// Encode each using myKey
      		phone_e0 = Passpack.encode("AES",phone_un0,myKey);
	      	phone_e1 = Passpack.encode("AES",phone_un1,myKey);
	      	phone_e2 = Passpack.encode("AES",phone_un2,myKey);
	      	phone_e3 = Passpack.encode("AES",phone_un3,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ phone_e0 + '&v2=' + phone_e1 + '&v3=' + phone_e2 + '&v4=' + phone_e3;
		
		} else if (category=="4"){
			// license
			license_un0 = $("input.license_e0").attr('value');
			license_un1 = $("input.license_e1").attr('value');
			license_un2 = $("input.license_e2").attr('value');
			license_un3 = nl2br($("textarea.license_e3").val(),1);
			
			// Encode each using myKey
      		license_e0 = Passpack.encode("AES",license_un0,myKey);
	      	license_e1 = Passpack.encode("AES",license_un1,myKey);
	      	license_e2 = Passpack.encode("AES",license_un2,myKey);
	      	license_e3 = Passpack.encode("AES",license_un3,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ license_e0 + '&v2=' + license_e1 + '&v3=' + license_e2 + '&v4=' + license_e3;
		
		} else if (category=="5"){
			// cards
			cards_un0 = $("input.cards_e0").attr('value');
			cards_un1 = $("input.cards_e1").attr('value');
			cards_un2 = $("input.cards_e2").attr('value');
			cards_un3 = $("input.cards_e3").attr('value');
			cards_un4 = $("input.cards_e4").attr('value');
			cards_un5 = $("input.cards_e5").attr('value');
			cards_un6 = $("input.cards_e6").attr('value');
			cards_un7 = $("input.cards_e7").attr('value');
			cards_un8 = nl2br($("textarea.cards_e8").val(),1);
			
			// Encode each using myKey
      		cards_e0 = Passpack.encode("AES",cards_un0,myKey);
	      	cards_e1 = Passpack.encode("AES",cards_un1,myKey);
	      	cards_e2 = Passpack.encode("AES",cards_un2,myKey);
	      	cards_e3 = Passpack.encode("AES",cards_un3,myKey);
	      	cards_e4 = Passpack.encode("AES",cards_un4,myKey);
	      	cards_e5 = Passpack.encode("AES",cards_un5,myKey);
	      	cards_e6 = Passpack.encode("AES",cards_un6,myKey);
	      	cards_e7 = Passpack.encode("AES",cards_un7,myKey);
	      	cards_e8 = Passpack.encode("AES",cards_un8,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ cards_e0 + '&v2=' + cards_e1 + '&v3=' + cards_e2 + '&v4=' + cards_e3 + '&v5=' + cards_e4 + '&v6=' + cards_e5 + '&v7=' + cards_e6 + '&v8=' + cards_e7 + '&v9=' + cards_e8;
		
		} else if (category=="6"){
			// banking
			banking_un0 = $("input.banking_e0").attr('value');
			banking_un1 = $("input.banking_e1").attr('value');
			banking_un2 = $("input.banking_e2").attr('value');
			banking_un3 = nl2br($("textarea.banking_e3").val(),1);
			
			// Encode each using myKey
      		banking_e0 = Passpack.encode("AES",banking_un0,myKey);
	      	banking_e1 = Passpack.encode("AES",banking_un1,myKey);
	      	banking_e2 = Passpack.encode("AES",banking_un2,myKey);
	      	banking_e3 = Passpack.encode("AES",banking_un3,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ banking_e0 + '&v2=' + banking_e1 + '&v3=' + banking_e2 + '&v4=' + banking_e3;
		
		} else if (category=="7"){
			// email
			email_un0 = $("input.email_e0").attr('value');
			email_un1 = $("input.email_e1").attr('value');
			email_un2 = $("input.email_e2").attr('value');
			email_un3 = $("input.email_e3").attr('value');
			email_un4 = $("input.email_e4").attr('value');
			email_un5 = nl2br($("textarea.email_e5").val(),1);
			
			// Encode each using myKey
      		email_e0 = Passpack.encode("AES",email_un0,myKey);
	      	email_e1 = Passpack.encode("AES",email_un1,myKey);
	      	email_e2 = Passpack.encode("AES",email_un2,myKey);
	      	email_e3 = Passpack.encode("AES",email_un3,myKey);
	      	email_e4 = Passpack.encode("AES",email_un4,myKey);
	      	email_e5 = Passpack.encode("AES",email_un5,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ email_e0 + '&v2=' + email_e1 + '&v3=' + email_e2 + '&v4=' + email_e3 + '&v5=' + email_e4 + '&v6=' + email_e5;
		
		} else if (category=="8"){
			// computer_user
			computer_user_un0 = $("input.computer_user_e0").attr('value');
			computer_user_un1 = $("input.computer_user_e1").attr('value');
			computer_user_un2 = $("input.computer_user_e2").attr('value');
			computer_user_un3 = $("input.computer_user_e3").attr('value');
			computer_user_un4 = nl2br($("textarea.computer_user_e4").val(),1);
			
			// Encode each using myKey
      		computer_user_e0 = Passpack.encode("AES",computer_user_un0,myKey);
	      	computer_user_e1 = Passpack.encode("AES",computer_user_un1,myKey);
	      	computer_user_e2 = Passpack.encode("AES",computer_user_un2,myKey);
	      	computer_user_e3 = Passpack.encode("AES",computer_user_un3,myKey);
	      	computer_user_e4 = Passpack.encode("AES",computer_user_un4,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ computer_user_e0 + '&v2=' + computer_user_e1 + '&v3=' + computer_user_e2 + '&v4=' + computer_user_e3 + '&v5=' + computer_user_e4;
		
		} else if (category=="9"){
			// hardware
			hardware_un0 = $("input.hardware_e0").attr('value');
			hardware_un1 = $("input.hardware_e1").attr('value');
			hardware_un2 = $("input.hardware_e2").attr('value');
			hardware_un3 = $("input.hardware_e3").attr('value');
			hardware_un4 = $("input.hardware_e4").attr('value');
			hardware_un5 = $("input.hardware_e5").attr('value');
			hardware_un6 = nl2br($("textarea.hardware_e6").val(),1);
			
			// Encode each using myKey
      		hardware_e0 = Passpack.encode("AES",hardware_un0,myKey);
	      	hardware_e1 = Passpack.encode("AES",hardware_un1,myKey);
	      	hardware_e2 = Passpack.encode("AES",hardware_un2,myKey);
	      	hardware_e3 = Passpack.encode("AES",hardware_un3,myKey);
	      	hardware_e4 = Passpack.encode("AES",hardware_un4,myKey);
	      	hardware_e5 = Passpack.encode("AES",hardware_un5,myKey);
	      	hardware_e6 = Passpack.encode("AES",hardware_un6,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ hardware_e0 + '&v2=' + hardware_e1 + '&v3=' + hardware_e2 + '&v4=' + hardware_e3 + '&v5=' + hardware_e4 + '&v6=' + hardware_e5 + '&v7=' + hardware_e6;
		
		} else if (category=="10"){
			// certificates
			certificates_un0 = $("input.certificates_e0").attr('value');
			certificates_un1 = $("input.certificates_e1").attr('value');
			certificates_un2 = $("input.certificates_e2").attr('value');
			certificates_un3 = $("input.certificates_e3").attr('value');
			certificates_un4 = nl2br($("textarea.certificates_e4").val(),1);
			certificates_un5 = nl2br($("textarea.certificates_e5").val(),1);
			
			// Encode each using myKey
      		certificates_e0 = Passpack.encode("AES",certificates_un0,myKey);
	      	certificates_e1 = Passpack.encode("AES",certificates_un1,myKey);
	      	certificates_e2 = Passpack.encode("AES",certificates_un2,myKey);
	      	certificates_e3 = Passpack.encode("AES",certificates_un3,myKey);
	      	certificates_e4 = Passpack.encode("AES",certificates_un4,myKey);
	      	certificates_e5 = Passpack.encode("AES",certificates_un5,myKey);
	      	
	      	queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ certificates_e0 + '&v2=' + certificates_e1 + '&v3=' + certificates_e2 + '&v4=' + certificates_e3 + '&v5=' + certificates_e4 + '&v6=' + certificates_e5;
		
		} else if (category=="11"){
			// notes
			notes_un0 = $("input.notes_e0").attr('value');
			notes_un3 = nl2br($("textarea.notes_e3").val(),1);
			
			// Encode each using myKey
      		notes_e0 = Passpack.encode("AES",notes_un0,myKey);
	      	notes_e3 = Passpack.encode("AES",notes_un3,myKey);
	      	
	      	var queryString = 'user_id=' + user + '&cat_name=' + cat_name + '&id=' + item_no + '&v1='+ notes_e0 + '&v2=' + notes_e1 + '&v3=' + notes_e2 + '&v4=' + notes_e3;
		
		}
		
		// Prevent addition if the title is blank
		var working_var = eval(cat_name + "_un0");

		if(!working_var) {
			$(".col4_row_edit_error").show();
			$("#item_blank").show();
			$(".loading").hide();
			return false;
		}

		// Call edit.php to make update on db
		$.ajax({
		type: "POST",
		url: "app/edit.php",
		data: queryString,
		success: function() {
			clear_status();
			$("#edit_" + cat_name).hide();
			$("#col4_title_edit").hide();
			$("#read_" + cat_name).show();
			$("#col4_title_read").show();
			get_list(0,category);
			read_db(category,item_no,"edit");
		}
		});
		
		// Hide the loading icon
		$(".loading").hide();
		
		time_out();
		return false;
	});
	
	// -------------------------------------------------------------
	// Add Mode - Show form and create new db record
	// -------------------------------------------------------------
	
	// Extract the id of the item just added, then show it in read mode
	function get_record_id(stat_msg) {

		populate = true;
		
		$.post("app/list_update.php", { user_id: user, cat_name: cat_name}, function(recordsXML){
			
			$('record',recordsXML).each(function(i){
				item_no = $(this).find("last_id").text();
			});
			get_list(0,category);
			read_db(category,item_no,stat_msg);
		});
	}
	
	$(".create_new_post").click(function() {
		
		// Show the loading icon
		clear_status();
		$(".loading").show();
	
		var addQueryString = "";
		
		// validate and process form here
		
		if (category=="1"){
			// web
			web_aun0 = $("input.web_a0").attr('value');
			web_aun1 = $("input.web_a1").attr('value');
			web_aun2 = $("input.web_a2").attr('value');
			web_aun3 = $("input.web_a3").attr('value');
			
			// add http://, if missing, to make a valid URL
			var http_regex = /^http[s]?:\/\//;
			
			if (web_aun3 == ""){
				web_aun3 = web_aun3;
			} else if (!web_aun3.match(http_regex)){
				web_aun3 = "http://" + web_aun3;
			}
			
			web_aun4 = nl2br($("textarea.web_a4").val(),1);
			
			// Encode each using myKey
      		web_a0 = Passpack.encode("AES",web_aun0,myKey);
	      	web_a1 = Passpack.encode("AES",web_aun1,myKey);
	      	web_a2 = Passpack.encode("AES",web_aun2,myKey);
	      	web_a3 = Passpack.encode("AES",web_aun3,myKey);
			web_a4 = Passpack.encode("AES",web_aun4,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ web_a0 + '&v2=' + web_a1 + '&v3=' + web_a2 + '&v4=' + web_a3 + '&v5=' + web_a4;
		
		} else if (category=="2"){
			// hosted
			hosted_aun0 = $("input.hosted_a0").attr('value');
			hosted_aun1 = $("input.hosted_a1").attr('value');
			hosted_aun2 = $("input.hosted_a2").attr('value');
			hosted_aun3 = $("input.hosted_a3").attr('value');
			hosted_aun4 = $("input.hosted_a4").attr('value');
			hosted_aun5 = $("input.hosted_a5").attr('value');
			hosted_aun6 = $("input.hosted_a6").attr('value');
			hosted_aun7 = $("input.hosted_a7").attr('value');
			hosted_aun8 = nl2br($("textarea.hosted_a8").attr('value'),1);
			
			// Encode each using myKey
      		hosted_a0 = Passpack.encode("AES",hosted_aun0,myKey);
	      	hosted_a1 = Passpack.encode("AES",hosted_aun1,myKey);
	      	hosted_a2 = Passpack.encode("AES",hosted_aun2,myKey);
	      	hosted_a3 = Passpack.encode("AES",hosted_aun3,myKey);
	      	hosted_a4 = Passpack.encode("AES",hosted_aun4,myKey);
	      	hosted_a5 = Passpack.encode("AES",hosted_aun5,myKey);
	      	hosted_a6 = Passpack.encode("AES",hosted_aun6,myKey);
	      	hosted_a7 = Passpack.encode("AES",hosted_aun7,myKey);
	      	hosted_a8 = Passpack.encode("AES",hosted_aun8,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ hosted_a0 + '&v2=' + hosted_a1 + '&v3=' + hosted_a2 + '&v4=' + hosted_a3 + '&v5=' + hosted_a4 + '&v6=' + hosted_a5 + '&v7=' + hosted_a6 + '&v8=' + hosted_a7 + '&v9=' + hosted_a8;
		
		} else if (category=="3"){
			// phone
			phone_aun0 = $("input.phone_a0").attr('value');
			phone_aun1 = $("input.phone_a1").attr('value');
			phone_aun2 = $("input.phone_a2").attr('value');
			phone_aun3 = nl2br($("textarea.phone_a3").val(),1);
			
			// Encode each using myKey
      		phone_a0 = Passpack.encode("AES",phone_aun0,myKey);
	      	phone_a1 = Passpack.encode("AES",phone_aun1,myKey);
	      	phone_a2 = Passpack.encode("AES",phone_aun2,myKey);
	      	phone_a3 = Passpack.encode("AES",phone_aun3,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ phone_a0 + '&v2=' + phone_a1 + '&v3=' + phone_a2 + '&v4=' + phone_a3;
		
		} else if (category=="4"){
			// license
			license_aun0 = $("input.license_a0").attr('value');
			license_aun1 = $("input.license_a1").attr('value');
			license_aun2 = $("input.license_a2").attr('value');
			license_aun3 = nl2br($("textarea.license_a3").val(),1);
			
			// Encode each using myKey
      		license_a0 = Passpack.encode("AES",license_aun0,myKey);
	      	license_a1 = Passpack.encode("AES",license_aun1,myKey);
	      	license_a2 = Passpack.encode("AES",license_aun2,myKey);
	      	license_a3 = Passpack.encode("AES",license_aun3,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ license_a0 + '&v2=' + license_a1 + '&v3=' + license_a2 + '&v4=' + license_a3;
		
		} else if (category=="5"){
			// cards
			cards_aun0 = $("input.cards_a0").attr('value');
			cards_aun1 = $("input.cards_a1").attr('value');
			cards_aun2 = $("input.cards_a2").attr('value');
			cards_aun3 = $("input.cards_a3").attr('value');
			cards_aun4 = $("input.cards_a4").attr('value');
			cards_aun5 = $("input.cards_a5").attr('value');
			cards_aun6 = $("input.cards_a6").attr('value');
			cards_aun7 = $("input.cards_a7").attr('value');
			cards_aun8 = nl2br($("textarea.cards_a8").val(),1);
			
			// Encode each using myKey
      		cards_a0 = Passpack.encode("AES",cards_aun0,myKey);
	      	cards_a1 = Passpack.encode("AES",cards_aun1,myKey);
	      	cards_a2 = Passpack.encode("AES",cards_aun2,myKey);
	      	cards_a3 = Passpack.encode("AES",cards_aun3,myKey);
	      	cards_a4 = Passpack.encode("AES",cards_aun4,myKey);
	      	cards_a5 = Passpack.encode("AES",cards_aun5,myKey);
	      	cards_a6 = Passpack.encode("AES",cards_aun6,myKey);
	      	cards_a7 = Passpack.encode("AES",cards_aun7,myKey);
	      	cards_a8 = Passpack.encode("AES",cards_aun8,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ cards_a0 + '&v2=' + cards_a1 + '&v3=' + cards_a2 + '&v4=' + cards_a3 + '&v5=' + cards_a4 + '&v6=' + cards_a5 + '&v7=' + cards_a6 + '&v8=' + cards_a7 + '&v9=' + cards_a8;
		
		} else if (category=="6"){
			// banking
			banking_aun0 = $("input.banking_a0").attr('value');
			banking_aun1 = $("input.banking_a1").attr('value');
			banking_aun2 = $("input.banking_a2").attr('value');
			banking_aun3 = nl2br($("textarea.banking_a3").val(),1);
			
			// Encode each using myKey
      		banking_a0 = Passpack.encode("AES",banking_aun0,myKey);
	      	banking_a1 = Passpack.encode("AES",banking_aun1,myKey);
	      	banking_a2 = Passpack.encode("AES",banking_aun2,myKey);
	      	banking_a3 = Passpack.encode("AES",banking_aun3,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ banking_a0 + '&v2=' + banking_a1 + '&v3=' + banking_a2 + '&v4=' + banking_a3;
		
		} else if (category=="7"){
			// email
			email_aun0 = $("input.email_a0").attr('value');
			email_aun1 = $("input.email_a1").attr('value');
			email_aun2 = $("input.email_a2").attr('value');
			email_aun3 = $("input.email_a3").attr('value');
			email_aun4 = $("input.email_a4").attr('value');
			email_aun5 = nl2br($("textarea.email_a5").val(),1);
			
			// Encode each using myKey
      		email_a0 = Passpack.encode("AES",email_aun0,myKey);
	      	email_a1 = Passpack.encode("AES",email_aun1,myKey);
	      	email_a2 = Passpack.encode("AES",email_aun2,myKey);
	      	email_a3 = Passpack.encode("AES",email_aun3,myKey);
	      	email_a4 = Passpack.encode("AES",email_aun4,myKey);
	      	email_a5 = Passpack.encode("AES",email_aun5,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ email_a0 + '&v2=' + email_a1 + '&v3=' + email_a2 + '&v4=' + email_a3 + '&v5=' + email_a4 + '&v6=' + email_a5;
		
		} else if (category=="8"){
			// computer_user
			computer_user_aun0 = $("input.computer_user_a0").attr('value');
			computer_user_aun1 = $("input.computer_user_a1").attr('value');
			computer_user_aun2 = $("input.computer_user_a2").attr('value');
			computer_user_aun3 = $("input.computer_user_a3").attr('value');
			computer_user_aun4 = nl2br($("textarea.computer_user_a4").val(),1);
			
			// Encode each using myKey
      		computer_user_a0 = Passpack.encode("AES",computer_user_aun0,myKey);
	      	computer_user_a1 = Passpack.encode("AES",computer_user_aun1,myKey);
	      	computer_user_a2 = Passpack.encode("AES",computer_user_aun2,myKey);
	      	computer_user_a3 = Passpack.encode("AES",computer_user_aun3,myKey);
	      	computer_user_a4 = Passpack.encode("AES",computer_user_aun4,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ computer_user_a0 + '&v2=' + computer_user_a1 + '&v3=' + computer_user_a2 + '&v4=' + computer_user_a3 + '&v5=' + computer_user_a4;
		
		} else if (category=="9"){
			// hardware
			hardware_aun0 = $("input.hardware_a0").attr('value');
			hardware_aun1 = $("input.hardware_a1").attr('value');
			hardware_aun2 = $("input.hardware_a2").attr('value');
			hardware_aun3 = $("input.hardware_a3").attr('value');
			hardware_aun4 = $("input.hardware_a4").attr('value');
			hardware_aun5 = $("input.hardware_a5").attr('value');
			hardware_aun6 = nl2br($("textarea.hardware_a6").val(),1);
			
			// Encode each using myKey
      		hardware_a0 = Passpack.encode("AES",hardware_aun0,myKey);
	      	hardware_a1 = Passpack.encode("AES",hardware_aun1,myKey);
	      	hardware_a2 = Passpack.encode("AES",hardware_aun2,myKey);
	      	hardware_a3 = Passpack.encode("AES",hardware_aun3,myKey);
	      	hardware_a4 = Passpack.encode("AES",hardware_aun4,myKey);
	      	hardware_a5 = Passpack.encode("AES",hardware_aun5,myKey);
	      	hardware_a6 = Passpack.encode("AES",hardware_aun6,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ hardware_a0 + '&v2=' + hardware_a1 + '&v3=' + hardware_a2 + '&v4=' + hardware_a3 + '&v5=' + hardware_a4 + '&v6=' + hardware_a5 + '&v7=' + hardware_a6;
		
		} else if (category=="10"){
			// certificates
			certificates_aun0 = $("input.certificates_a0").attr('value');
			certificates_aun1 = $("input.certificates_a1").attr('value');
			certificates_aun2 = $("input.certificates_a2").attr('value');
			certificates_aun3 = $("input.certificates_a3").attr('value');
			certificates_aun4 = nl2br($("textarea.certificates_a4").val(),1);
			certificates_aun5 = nl2br($("textarea.certificates_a5").val(),1);
			
			// Encode each using myKey
      		certificates_a0 = Passpack.encode("AES",certificates_aun0,myKey);
	      	certificates_a1 = Passpack.encode("AES",certificates_aun1,myKey);
	      	certificates_a2 = Passpack.encode("AES",certificates_aun2,myKey);
	      	certificates_a3 = Passpack.encode("AES",certificates_aun3,myKey);
	      	certificates_a4 = Passpack.encode("AES",certificates_aun4,myKey);
	      	certificates_a5 = Passpack.encode("AES",certificates_aun5,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ certificates_a0 + '&v2=' + certificates_a1 + '&v3=' + certificates_a2 + '&v4=' + certificates_a3 + '&v5=' + certificates_a4 + '&v6=' + certificates_a5;
		
		} else if (category=="11"){
			// notes
			notes_aun0 = $("input.notes_a0").attr('value');
			notes_aun3 = nl2br($("textarea.notes_a3").val(),1);
			
			// Encode each using myKey
      		notes_a0 = Passpack.encode("AES",notes_aun0,myKey);
	      	notes_a3 = Passpack.encode("AES",notes_aun3,myKey);
	      	
	      	addQueryString = 'user_id=' + user + '&cat_name=' + cat_name + '&v1='+ notes_a0 + '&v2=' + notes_a1 + '&v3=' + notes_a2 + '&v4=' + notes_a3;
		
		}
		
		// Prevent addition if the title is blank
		
		var working_var = eval(cat_name + "_aun0");
		
		if(!working_var) {
			$(".col4_row_edit_error").show();
			$("#item_blank").show();
			$(".loading").hide();
			return false;
		}
		
		// Call write.php to make update on db
		$.ajax({
		type: "POST",
		url: "app/add.php",
		data: addQueryString,
		success: function() {
			clear_status();
			update_count(1);
			$("#add_" + cat_name).hide();
			$("#col4_title_add").hide();
			$("#read_" + cat_name).show();
			$("#col4_title_read").show();
			get_record_id("add");
		}
		});
		
		// Hide the loading icon
		$(".loading").hide();
		
		time_out();
		return false;

	});
	
	// -------------------------------------------------------------
	// Delete mode
	// -------------------------------------------------------------
	
	$(".confirm_delete").click(function () {
		
		deleteQueryString = "id=" + item_no + "&cat_name=" + cat_name + "&user_id=" + user;
		
		$.ajax({
			type: "POST",
			url: "app/delete.php",
			data: deleteQueryString,
			success: function() {		
				//read_db();
				clear_status();
				update_count(0);
				$("#read_" + cat_name).hide();
				$("#edit_" + cat_name).hide();
				$("#col4_empty").show();
				$(".lightbox_bg").hide();
				$("#delete_confirm").hide();
				//$("#delete_success").show();
				get_list(0,category,"delete");	
			}
		});
		time_out();
		return false;
	});
	
	// -------------------------------------------------------------
	// Toggle Read/Edit modes
	// -------------------------------------------------------------

	$(".edit").click(function () {
		clear_status();
		$("#read_" + cat_name).hide();
		$("#add_" + cat_name).hide();
		$("#col4_title_read").hide();
		$("#edit_" + cat_name).show();
		$("#col4_title_edit").show();
		$("input." + cat_name + "_e0").focus();
		time_out();
	});
	
	$(".close").click(function () {
		clear_status();
		$("#edit_" + cat_name).hide();
		$("#col4_title_edit").hide();
		$("#read_" + cat_name).show();
		$("#col4_title_read").show();
		time_out();
	});
	
	// -------------------------------------------------------------
	// Open/Hide Create new form
	// -------------------------------------------------------------

	$("#create_new").click(function () {
		$("input").attr("value","");
		$("textarea").attr("value","");
		$("#detail_empty").hide();
		$("#col4_title_empty").hide();
		$("#col4_title_read").hide();
		$("#col4_title_edit").hide();
		$("#read_" + cat_name).hide();
		$("#edit_" + cat_name).hide();
		$("#add_" + cat_name).show();
		$("#col4_title_add").show();
		clear_status();
		$("input." + cat_name + "_a0").focus();
		time_out();
	});
	
	$(".close_create_new").click(function () {
		clear_status();
		$("#edit_" + cat_name).hide();
		$("#add_" + cat_name).hide();
		if (item_no == "0") {
			$("#detail_empty").show();
			$("#col4_title_add").hide();
			$("#col4_title_empty").show();
		} else {
			$("#read_" + cat_name).show();
			$("#col4_title_add").hide();
			$("#col4_title_read").show();
		}
		time_out();
	});

	// -------------------------------------------------------------
	// Confirm delete
	// -------------------------------------------------------------
	
	
	$(".delete").click(function () {
		$(".lightbox_bg").fadeIn("fast");
		$(".modal").fadeIn("fast");
		time_out();
	});
	
	$("#cancel_delete").click(function () {
		$(".modal").fadeOut("fast");
		$(".lightbox_bg").fadeOut("fast");
		time_out();
	});
	
	// -------------------------------------------------------------
	// Unlock
	// -------------------------------------------------------------
	
	// Replace user-typed main key with SHA256 hash and use that
	
	function hash_key(){
		myKey = $("input#master_key_input").val();
		var hashed = Passpack.encode("SHA256",myKey);
		myKey = hashed;
		$("input#master_key_input").val("");
	}
	
	function unlock(){
	
		clear_status();
		$(".loading").show();
		
		hash_key();
		
		// Check master key
		var check = "checker";
		var encoded_check = "";
		
		// Ajax call
		$.post("app/checker.php", { id: user }, function(checkerXML){
			$('validator', checkerXML).each(function(i){		
				// Extract the XML variable 
				encoded_check = $(this).find("check").text();
				// Decode each using myKey
				checked = Passpack.decode("AES",encoded_check,myKey);
				
			});
			// Perform the check
			if (checked == check){
				get_categories(user,category);
				$("#master_key").hide();
				$('#main').css("background-image","url(../img/core/bg_secure.gif)");
				$('#container').css("background-image","url(../img/core/bg_secure.gif)");
				$("#left").fadeIn("fast");
				$("#middle").fadeIn("fast");
				$("#detail_empty").show();
				$("#col_container_locked").hide();
				$("#col_container_secure").show();
				$("#login_status_locked").hide();
				$("#login_status_unlocked").show();
				$("#create_new").show();
				time_out();
			}
			else {
				// error
				$("#key_message").hide();
				$("#key_error").show();
			}
			
		});
		
		$(".loading").hide();
	}
	
	$("#unlock").click(function () {
		unlock();
	});
	
	// Prevent return/enter displaying the value of myKey.
	$("#master_key").bind("keypress", function(e) {
        if (e.keyCode == 13) {
        	unlock();
        	return false;
        }
 	});
	
	// -------------------------------------------------------------
	// Lock
	// -------------------------------------------------------------
	
	function lock_out() {
		myKey = "";
		clear_status();
		reset_vars();
		$("#item_list").replaceWith("<ul id=\"item_list\"></ul>");
		$("#categories").replaceWith("<ul id=\"categories\">Empty</ul>");
		$("#read_" + cat_name).hide();
		$("#edit_" + cat_name).hide();
		$("#add_" + cat_name).hide();
		$("#col_container_secure").hide();
		$("#col_container_locked").show();
		$("#create_new").hide();
		$("#login_status_unlocked").hide();
		$("#login_status_locked").show();
		$('#main').css("background-image","url(../img/core/bg_login.gif)");
		$('#container').css("background-image","url(../img/core/bg_login.gif)");
		setup_page();
	}
	
	$("#lock").click(function () {
		lock_out();
	});
	
	// -------------------------------------------------------------
	// Time-out
	// -------------------------------------------------------------
	
	function time_out(){
	
		if(lock_out_time){
			$(this).stopTime();
			$(this).oneTime(lock_out_time, 'controlled', function() {
				lock_out();
			});
		} else {
			return false;
		}
	}	
	
	// -------------------------------------------------------------
	// Toggle show/hide password
	// -------------------------------------------------------------
	
	$(".pwd_show").click(function () {
		reveal_password = eval(cat_name + "_r2");
		$("." + cat_name + "_r2").replaceWith("<span class=\"" + cat_name + "_r2\">" + reveal_password + "</span>");
		$(".pwd_show").hide();
		$(".pwd_hide").show();
	});
	
	$(".pwd_hide").click(function () {
		$("." + cat_name + "_r2").replaceWith("<span class=\"" + cat_name + "_r2\">&bull; &bull; &bull; &bull; &bull; &bull;</span>");
		$(".pwd_hide").hide();
		$(".pwd_show").show();
	});
	
	// -------------------------------------------------------------
	// Password Generator
	// -------------------------------------------------------------
	
	$(".pwd_gen_8").click(function () {
		var password = $.password(8);
		$("input." + cat_name + "_e2").attr("value", password);
		$("input." + cat_name + "_a2").attr("value", password);
	});
	
	$(".pwd_gen_12").click(function () {
		var password = $.password(12);
		$("input." + cat_name + "_e2").attr("value", password);
		$("input." + cat_name + "_a2").attr("value", password);
	});
	
	// -------------------------------------------------------------
	// Reset scrolling divs onrezise
	// -------------------------------------------------------------
	
	function track_viewport_height() {
		viewport_height = $(window).height();
		return viewport_height;
	}
	
	function resize_scrollers() {
		$('.scroll_holder').css("height",viewport_height-60);
		$('#col4_container').css("height",viewport_height-60);
	}
	
	$(window).resize(function() {
		track_viewport_height();
  		resize_scrollers()
	});

	
// Close document.ready
});